import adModel from "../../../main/view/adView/adModel";
import { ZYEventMgr } from "../../manager/YLEventMgr";
import MyTools from "../../util/MyTools";
import { WXAPIMgr } from "../concrete/WXAPIManager";


/**
 * 
 * @author: xj.huang
 * @date: 2021/08/03 19:51:23
 */
 export default class wxCellBanner {
    constructor() {
        wxCellBanner._instance = this;
    }
    private serverCustomArr = []
    private serverGirdArr = []
    private currCustomGrid: wx.CustomAd;
    private currGridGrid: wx.CustomAd;
    private static _instance: wxCellBanner;
    public static get instance(): wxCellBanner {
        if (!this._instance) {
            this._instance = new wxCellBanner();
        }
        return this._instance;
    }
    /**初始化微信广告 */
    public initWxBannerAd() {
        this.onAddEvent()
        this.serverCustomArr = adModel.gameConfig.adv_unit_conf.grid || [];
        this.serverGirdArr = adModel.gameConfig.adv_unit_conf.custom || [];
        //根据后台配置刷新
        this.initCustomAd()
        this.initGridAd()
    }
    /**事件监听发送 */
    private onAddEvent(){
        ZYEventMgr.on(ADEvent.SHOW_GIRID_AD, this, this.showCustomAd)
        ZYEventMgr.on(ADEvent.HIDE_GIRID_AD, this, this.hideCustomAd)
    }
    /**显示格子广告-多个格子 */
    private async initCustomAd() {
        if (this.serverCustomArr.length <= 0 || this.currCustomGrid) return
        let showId = this.serverCustomArr[MyTools.getRandomNumInt(0, this.serverCustomArr.length - 1)]
        this.currCustomGrid = await WXAPIMgr.createCustomAd(showId, true);
        if (this.currCustomGrid) {
           this.showCustomAd()
        } else if (this.currCustomGrid === undefined) {
            Laya.timer.once(3000, this, this.initGridAd)
        }
        console.log("this.currGrid", this.currCustomGrid, showId)
    }
    /**显示格子广告-单个个格子 */
    private async initGridAd() {
        if (this.serverGirdArr.length <= 0 || this.currGridGrid) return;
        this.currGridGrid = await WXAPIMgr.createCustomAd(this.serverGirdArr[0], false);
        if (this.currGridGrid) {
            this.showGirdAd()
        } else if (this.currGridGrid === undefined) {
            Laya.timer.once(3000, this, this.initGridAd)
        }
        console.log("this.currGrid", this.currGridGrid, this.serverGirdArr[0])
        return this.currGridGrid

    }
    /**显示底部广告格子 （多格子）*/
    public showCustomAd() {
        if(adModel.isFocreHiddeBanner) return
        if (this.currCustomGrid&&!adModel.bannerShowType) {
            this.currCustomGrid.show();
            adModel.gridShowType=true;
            ZYEventMgr.emit(ADEvent.HIDE_BOTTOM_AD)
        }else if(this.currCustomGrid&&adModel.bannerShowType){
            this.currCustomGrid.hide()
            adModel.gridShowType=false;
            ZYEventMgr.emit(ADEvent.SHOW_BOTTOM_AD)
        } else {
            console.warn("无底部格子广告")
            ZYEventMgr.emit(ADEvent.SHOW_BOTTOM_AD)
        }
    }
    /**隐藏底部广告格子 */
    public hideCustomAd() {
        this.currCustomGrid && this.currCustomGrid.hide()
        if(adModel.isFocreHiddeBanner) ZYEventMgr.emit(ADEvent.SHOW_BOTTOM_AD)
    }
    /**显示右边广告格子 (单格子) */
    public showGirdAd() {
       
        if (this.currGridGrid) {
            // console.log("操蛋",adModel.isFocreHiddeBanner,adModel.bannerShowType)
            this.currGridGrid.show()
        } else {
            console.warn("无单个格子广告")
        }
    }
    /**隐藏右边广告格子 */
    public hideGirdAd() {
        this.currGridGrid && this.currGridGrid.hide()
    }
}
// export const wxCellMgr = wxCellBanner.instance;